import { createRouter, createWebHistory } from 'vue-router'
import Service from '../components/content/Service.vue'
import Dynamic from '../components/content/Dynamic.vue'
import Play from '../components/content/Footer/Play.vue'
import Talk from '../components/content/Footer/Talk.vue'

const routes = [
  {
    path: '/myhome',
    component: () => import('../views/homeone.vue')
  },
  {
    path: '/chatlisth5',
    component: () => import('../views/chatlisth5.vue')
  },
  {
    path: '/list',
    component: () => import('../views/list.vue')
  },
  {
    path: '/content',
    component: () => import('../views/content.vue'),
    children: [
      {

        path: '/content/service',
        component: Service
      },
      {
        path: '/content/dynamic',
        component: Dynamic
      },
      {
        path: '/content',
        redirect: '/content/service'
      }
    ]
  },
  {
    path: '/content/play',
    component: Play
  },
  {
    path: '/content/talk',
    component: Talk
  },
  {
    path: '/mycenter',
    component: () => import('../views/mycenter.vue'),
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/liaotianshi',
    component: () => import('../views/liantianshi.vue'),
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/search',
    component: () => import('../views/search.vue')
  },
  {
    path: '/:xxx(.*)*',
    redirect: '/myhome'
  },
  {
    path: '/',
    redirect: '/myhome'
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  // 而不是去检查每条路由记录
  // to.matched.some(record => record.meta.requiresAuth)
  if (to.meta.requiresAuth) {
    // 此路由需要授权，请检查是否已登录
    // 如果没有，则重定向到登录页面
    if (localStorage.getItem('username')) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
